import win32gui
import win32con
import json

from common.log.log import log

hwnd_title = {}

"""
Windows接口功能模块
"""


def get_all_hwnd(hwnd, mouse):
    if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd):
        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})


# 关闭窗口
def close_windows_By_Text(name):
    win32gui.EnumWindows(get_all_hwnd, 0)
    for h, t in hwnd_title.items():
        if name in t:
            win32gui.SendMessage(h, win32con.WM_CLOSE)
            log.info(f"关闭窗口{name}成功")
            return True


def close_windows_withoutMainFrame(name):
    win32gui.EnumWindows(get_all_hwnd, 0)
    for h, t in hwnd_title.items():
        if name in t and "InoRobotLab" not in t:
            win32gui.SendMessage(h, win32con.WM_CLOSE)
            log.info(f"关闭窗口{name}成功")
            return True


def get_windows(name):
    win32gui.EnumWindows(get_all_hwnd, 0)
    for h, t in hwnd_title.items():
        if name in t:
            return True
    return False


def max_Window(name):
    win32gui.EnumWindows(get_all_hwnd, 0)
    for h, t in hwnd_title.items():
        if name in t:
            win32gui.ShowWindow(h, win32con.SW_SHOW)
            log.info(f"关闭窗口{name}成功")
            return True


# dict-list-dict-list
def read_json_file():
    with open(r"E:\02_02_01\newprj\Data\BreakPoints.jsn",
              "r", encoding="utf-8") as file:
        jsonFile = json.load(file)  # it is a dict
        print(jsonFile)
        print(jsonFile["ProgramsBreaks"])
        print(type(jsonFile))


if __name__ == '__main__':
    pass
